import { createRouter, createWebHashHistory } from "vue-router"
import { showToast } from "vant"

const router = createRouter({
  // 路由模式(hash模式)
  history: createWebHashHistory(),
  // 路由规则
  routes: [
    // {
    //   path: '/',
    //   component: () => import('../views/home/HomeView.vue')
    // },
    // {
    //   path: '/home',
    //   component: () => import('../views/home/HomeView.vue')
    // },
    {
      path: "/:catchAll(.*)",
      redirect: "/home"
    },
    {
      // name: 'home',
      path: '/home',
      // alias: '/',
      component: () => import('../views/home/HomeView.vue'),
    },
    {
      path: '/order',
      component: () => import('../views/order/OrderView.vue'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/cart',
      component: () => import('../views/cart/CartView.vue'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/mine',
      component: () => import('../views/mine/MineView.vue'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/store',
      component: () => import('../views/store/StoreView.vue')
    },
    {
      path: '/createorder',
      component: () => import('../views/createOrder/CreateOrder.vue'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/address',
      component: () => import('../views/address/AddressView.vue'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/addressedit',
      component: () => import('../views/addressEdit/AddressEdit.vue'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/userinfoedit',
      component: () => import('../views/userInfoEdit/UserInfoEdit.vue'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/login',
      component: () => import('../views/login/LoginView.vue')
    },
    {
      path: '/register',
      component: () => import('../views/register/RegisterView.vue')
    },
  ]
})

router.beforeEach((to, from, next) => {
  if (to.meta.isAuth) {
    if (localStorage.isLogin === 'login') {
      next()
    } else {
      next('/login')
      showToast('请先登录~')
    }
  } else {
    next()
  }
})

export default router